package com.jit.app.frament;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.jit.app.R;
import com.jit.app.model.AppInfoModel;
import com.jit.app.model.Constants;
import com.jit.app.util.InstalledApplicationUtils;
import com.jit.app.util.JSONResult;
import com.jit.app.util.LoginUtils;

import java.util.List;

public class ManagerFragment extends Fragment {

    public static String tag = "manager";

    View view;
    AppInfoModel model = new AppInfoModel();

    Handler handler;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_manager, null);

        initView();

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch(msg.what){
                    case Constants.MessageWhat.APPINFO_LOAD_SUCCESS:
                        JSONResult jsonResult = (JSONResult) msg.obj;
                        if(jsonResult.getCode() == Constants.ResultCode.SUCCESS){
                            List<JSONObject> appList = ((JSONArray)jsonResult.getData().get("installAppInfoList")).toJavaList(JSONObject.class);
                            List<JSONObject> newVersionAppList = InstalledApplicationUtils.getNewVersionAppList(getActivity(), appList);
                        }
                        break;
                }
                return false;
            }
        });

        initData();
        return view;
    }

    ImageView headPicImageView;
    TextView nickNameTextView;

    private void initView(){
        headPicImageView = view.findViewById(R.id.iv_headPic);
        nickNameTextView = view.findViewById(R.id.tv_nickname);

        String userInfo = LoginUtils.isLogin(getActivity());
        if(userInfo != null){
            JSONObject user = JSONObject.parseObject(userInfo);
            String uri = user.getString("picPath");
            if(uri != null && !uri.equals("")){
                Glide.with(getActivity()).load(Constants.HOST.FILE_HOST+uri).into(headPicImageView);
            }
            String nickName = user.getString("nickName");
            if(nickName != null && !nickName.equals("")){
                nickNameTextView.setText(nickName);
            }else{
                nickNameTextView.setText(user.getString("username"));
            }
        }
    }

    private void initData(){
        List<String> packs = InstalledApplicationUtils.getAllInstalledAppPackageName(getActivity());
        model.loadInstalledAppList(handler,packs);
    }
}
